package com.ledim.widget.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.letv.android.young.client.R;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LogUtil;

public class HomeXListViewFooter extends FrameLayout {

    public final static int STATE_NORMAL = 0;
    public final static int STATE_READY = 1;
    public final static int STATE_LOADING = 2;
    public final static int STATE_OVER = 3;

    private Context mContext;

    private View mContentView;
    private View mLoad;
    private ImageView mProgressBar;
    private TextView mHintView;
    private AnimationDrawable mAnimationDrawable = null;
    private AnimationDrawable mLoadAnimationDrawable = null;
    private View ll_end;
    private int mBottomHeight;
    private int mMaxLegHeight;

    public HomeXListViewFooter(Context context) {
        super(context);
        initView(context);
    }

    public HomeXListViewFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeXListViewFooter(Context context, AttributeSet attrs, int deFStyle) {
        super(context, attrs, deFStyle);
        initView(context);
    }

    private int mState;

    public void setState(int state) {
        if (mState == state) {
            return;
        }
        mHintView.setVisibility(View.VISIBLE);
        //		mProgressBar.setVisibility(View.INVISIBLE);
        mHintView.setTextColor(Color.parseColor("#333333"));
        if (mLoadAnimationDrawable.isRunning()) {
            mLoadAnimationDrawable.stop();
        }

        mState = state;
        if (state == STATE_READY) {
            mLoad.setVisibility(View.VISIBLE);
            ll_end.setVisibility(View.GONE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_ready);
            mProgressBar.setVisibility(View.GONE);
        } else if (state == STATE_LOADING) {
            //			mProgressBar.setVisibility(View.VISIBLE);
            mHintView.setText("大波内容正在赶来…");
            mProgressBar.setVisibility(View.VISIBLE);
            ll_end.setVisibility(GONE);
            mLoadAnimationDrawable.start();
        } else if (state == STATE_OVER) {
            mLoad.setVisibility(View.GONE);
            ll_end.setVisibility(View.VISIBLE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_over);
            mHintView.setTextColor(Color.parseColor("#888888"));
            mProgressBar.setVisibility(View.GONE);

        } else {
            mLoad.setVisibility(View.VISIBLE);
            ll_end.setVisibility(View.GONE);
            mHintView.setVisibility(View.VISIBLE);
            mHintView.setText(R.string.xlistview_footer_hint_normal);
            mProgressBar.setVisibility(View.GONE);
        }
    }

    public void setBottomMargin(int height) {
        //if (height < 0) {
        //    return;
        //}
        LogUtil.e("ccx setBottomMargin:" + height);
        mBottomHeight = height;
        if (mState == STATE_OVER && height > DensityUtil.dip2px(getContext(), 30) &&
                height <= mMaxLegHeight) {
            ll_end.setVisibility(VISIBLE);
            // FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams();
            ll_end.getLayoutParams().height = height;
            ll_end.requestLayout();
        }

        //return;
        //if (height < 10) {
        //    mImage.setImageResource(R.drawable.pull_leg);
        //}
    }

    public int getBottomMargin() {
        return mBottomHeight;
    }

    /**
     * normal status
     */
    public void normal() {
        mHintView.setText("查看更多");
        //		mProgressBar.setVisibility(View.GONE);
    }

    /**
     * loading status
     */
    public void loading() {
        mHintView.setText("加载中...");
        //		mProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * hide footer when disable pull load more
     */
    public void hide() {
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = 0;
        mContentView.setLayoutParams(lp);
    }

    /**
     * show footer
     */
    public void show() {
        FrameLayout.LayoutParams lp = (LayoutParams) mContentView.getLayoutParams();
        lp.height = LayoutParams.WRAP_CONTENT;
        mContentView.setLayoutParams(lp);
    }

    FrameLayout moreView;

    private void initView(Context context) {
        mContext = context;
        moreView = (FrameLayout) LayoutInflater.from(mContext)
                                               .inflate(R.layout.home_xlistview_footer, null);
        addView(moreView);
        moreView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        mContentView = moreView.findViewById(R.id.xlistview_footer_content);
        mLoad = moreView.findViewById(R.id.xlistview_footer_load);
        mProgressBar = (ImageView) moreView.findViewById(R.id.xlistview_footer_progressbar);
        mHintView = (TextView) moreView.findViewById(R.id.xlistview_footer_hint_textview);
        ll_end = moreView.findViewById(R.id.ll_end);
        Bitmap bitmap =
                BitmapFactory.decodeResource(this.getResources(), R.drawable.pull_leg_bottom);
        mMaxLegHeight = bitmap.getHeight();
        mProgressBar.setImageResource(R.anim.refresh_animation);
        mLoadAnimationDrawable = (AnimationDrawable) mProgressBar.getDrawable();
        mLoadAnimationDrawable.setOneShot(false);
    }

    public int getVisiableHeight() {
        return moreView.getHeight();
    }

}
